// Console 해제

if(typeof window.console=='undefined'){ window.console={log:function(){},error:function(){}}}else{var consoleAPI;if(typeof console._commandLineAPI!=='undefined'){console.API=console._commandLineAPI}else if(typeof console._inspectorCommandLineAPI!=='undefined'){console.API=console._inspectorCommandLineAPI}};

(function(window, undefined) {

console.log( "Javascript Class - version 0.4.1" );

// Class Genetater Factory
var Class = function( options ) {
	var _instance, _class, _parent, _constructor;
	var className = ( typeof options.name == "string" ) ? options.name.replace(/[^a-z_]/gi, '') : "__CLASS__";	
	
	//_class = function __CLASS__() { _instance = this; return create.apply( _instance, arguments ); };
	_class = eval('function '+className+'() { _instance = this; return _nativeCode.apply( _instance, arguments ); };'+className );
	
	_parent = ( typeof options.parent == "function" ) ? function( parent ) {
			console.log( "Class create [", className, "] from [", parent.name, "]" );
			return parent;
		}( options.parent ): 
		function( parentName ) { 
			console.log( "Class create [", className, "] from [", parentName, "]" );
			return ( typeof Class[parentName] == "function" && Class[parentName].prototype.__ancestor === Core ) ? Class[parentName] : Core; 
		}( ( typeof options.parent == "string" ) ? options.parent : "Core" );

	_constructor = ( options.constructor == undefined ) ? function() {} : options.constructor;
	
	// Prototype
	_class.prototype.include = _class.include = Class.include;
	_class.prototype.extend = _class.extend = Class.extend;
	_class.include( _parent.prototype );
	
	_class.prototype.__class = _class;
	_class.prototype.__parent = _parent;
	_class.prototype.__ancestor = _parent.prototype.__ancestor;
	_class.prototype.constructor = _class;
	
	_class.extend( _parent, _class );
	
	var _nativeCode = function() {
		
		var parent = _parent.apply( _instance, arguments );
		var constructor = ((typeof _constructor == "function") ? _constructor.apply( _instance, arguments ) : _constructor);
		
		var _super = {};
		for ( var name in parent ) {
			if ( name == "_super" ) { }
			else {
				_super[name] = parent[name];
			};
	    };
	    
	    _instance._super = _super;
		_instance.extend( _instance._super );
		_instance.extend( constructor );
		
		if ( _instance.__construct ) {
			_instance.__construct.apply( _instance, arguments );
		};
		
		return _instance;
	};
	
	return _class;
};

Class.microtime = function() {
	var timeFunc = Date.now || function() {return new Date().getTime();};
	return timeFunc() / 1000;
};

Class._startTime = Class.microtime();

Class.proxy = function( func, context ) { // context 를 유지해주는 함수
	context = ( context == undefined ) ? this : context;
	return (function() {
		func.apply( context, arguments );
	});
};

Class.extend = function( prop, context, ignoreOverride ) { // 확장 함수
	context = ( context == undefined ) ? this : context;
	
	for ( var name in prop ) {
		if ( ! ignoreOverride || context[name] == undefined ) {
			context[name] = prop[name];
		}
	};
};

Class.include = function( prop, context ) { // Prototype 확장 함수
	context = ( context == undefined ) ? this : context;
	
	var prototype = context.prototype;
	for ( var name in prop ) {
		prototype[name] = prop[name];
	};
};


// Core Class
var Core = function Core() {  return this; };

Core.prototype = {
	__construct: function() {}, // 생성시 실행 함수
	__destruct: function() {}, // 파괴시 실행 함수
	destroy: function() { // 파괴자 함수
		this.__destruct.apply( this, arguments );
	}
};

Core.prototype.include = Core.include = Class.include;
Core.prototype.extend = Core.extend = Class.extend;
Core.prototype.__class = Core;
Core.prototype.__parent = Object;
Core.prototype.__ancestor = Core;
Core.prototype.constructor = Core;
Class.Core = Core;


// Global Class
window.Class = Class;

// Global Helper
var Helper = function( constructor ) {
	
	var _instance, _helper, _nativeCode;
	
	_helper = function() { _instance = this; _nativeCode.apply( _instance, arguments ); };
	
	_nativeCode = function( target ) {
		_constructor = constructor.apply( _instance, arguments )
		
		Class.extend( _constructor, target, true );
	};

	return _helper;
};

window.Helper = Helper;

})(window);
